Page({
  data: {
      name: '', // 初始为空，用户可以输入
      time: '',
      textContent: '',
      draftId: null // 草稿ID，用于更新现有草稿
  },

  onLoad() {
      // 获取当前时间
      const now = new Date();
      const time = `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')} `;
      this.setData({
          time: time
      });
  },

  onInputText(event) {
      this.setData({
          textContent: event.detail.value
      });
  },

  onInputName(event) {
      this.setData({
          name: event.detail.value
      });
  },

  saveDraft() {
      const { textContent, name, draftId } = this.data;

      if (!textContent) {
          wx.showToast({
              title: '内容不能为空',
              icon: 'none'
          });
          return;
      }

      if (!name) {
          wx.showToast({
              title: '署名不能为空',
              icon: 'none'
          });
          return;
      }

      wx.request({
          url: '', 
          method: draftId ? 'PUT' : 'POST',
          data: {
              id: draftId,
              content: textContent,
              name: name,
              time: this.data.time
          },
          success: (res) => {
              if (res.statusCode === 200) {
                  wx.showToast({
                      title: '草稿保存成功',
                      icon: 'success'
                  });
                  this.setData({
                      draftId: res.data.id 
                  });
              } else {
                  wx.showToast({
                      title: '保存失败',
                      icon: 'none'
                  });
              }
          },
          fail: (err) => {
              console.error('保存草稿失败：', err);
              wx.showToast({
                  title: '网络错误',
                  icon: 'none'
              });
          }
      });
  },

  getWord: function() {
      wx.navigateTo({
        url: '/pages/getword/getword',
      })
  },
  getMusic: function() {
      wx.navigateTo({
        url: '/pages/getmusic/getmusic',
      })
  },
  getRecord() {
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      success: (res) => {
        var tempFilePaths = res.tempFilePaths;

             
        //将图片发送到后端
        tempFilePaths.forEach(function (filePath) {
            wx.uploadFile({
            url: 'http://localhost:8080/receiveInfo/receivesPictures',
              filePath: filePath,
              name: 'images',
              header: {
                'qrCode': 'cc123'
              },
              success: function (res) {
                var data = res.data // 上传成功后的返回结果
                // 处理返回结果
              }
            })
          })

   

      }
    });
  },
  getVideo: function() {
    
    //打开视频相册，获取视频
    wx.chooseVideo({
        sourceType: ['album', 'camera'],
        maxDuration: 60,
        success(res) {
          const tempFilePath = res.tempFilePath
          // 根据视频文件进行相关操作
          //将视频传到后端

          wx.uploadFile({
            url: 'http://localhost:8080/receiveInfo/receiveVideos',
            filePath: tempFilePath,
            name: 'files',
            header: {
                'qrCode': 'cc123'
              },
            success(uploadRes) {
              console.log(uploadRes.data);
            },
            fail(error) {
              console.error('上传失败:', error);
            }
          })

        }
      })

  }
});
